package com.horsen.service.edu.controller.web;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.horsen.common.util.R;
import com.horsen.service.base.model.VO.WebCourseOrderVO;
import com.horsen.service.edu.controller.BaseController;
import com.horsen.service.edu.model.Course;
import com.horsen.service.edu.model.DTO.WebCourseQueryDTO;
import com.horsen.service.edu.model.VO.ChapterVO;
import com.horsen.service.edu.model.VO.WebCourseInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程 控制器
 *
 * @author Horsen
 */
@Api(tags = "课程管理")
@ApiSupport(order = 2)
@RestController("WebCourse")
@RequestMapping("/web/edu/courses")
public class CourseController extends BaseController {

    @ApiOperationSupport(order = 1)
    @ApiOperation("按条件获取课程")
    @GetMapping("/")
    public R queryBy(@ApiParam("课程查询对象") WebCourseQueryDTO webCourseQueryDTO) {
        List<Course> courseList = courseService.queryBy(webCourseQueryDTO);
        return courseList != null ?
                R.ok("按条件获取课程成功").setData("courseList", courseList) : R.error("按条件获取课程失败");
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("根据ID获取一个课程")
    @GetMapping("/{id}")
    public R queryById(@ApiParam(value = "课程ID", required = true)
                       @PathVariable String id) {
        // 获取课程信息
        WebCourseInfoVO webCourseInfoVO = courseService.queryWebInfoById(id);
        // 获取这门课的章节信息
        List<ChapterVO> treeList = chapterService.getTreeList(id);
        return webCourseInfoVO != null ?
                R.ok("获取ID=" + id + "的课程成功").setData("course", webCourseInfoVO).setData("chapterList", treeList)
                : R.error("获取ID=" + id + "的课程失败");
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("根据ID获取一个课程订单VO")
    @GetMapping("/order/{id}")
    public WebCourseOrderVO getCourseOrderById(@ApiParam(value = "课程ID", required = true)
                                               @PathVariable String id) {
        return courseService.getCourseOrderById(id);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("根据ID增加课程销量")
    @PutMapping("/{id}")
    public R addBuyCount(@ApiParam(value = "课程ID", required = true)
                         @PathVariable String id) {
        return courseService.addBuyCount(id) ?
                R.ok("增加课程销量成功") : R.error("增加课程销量失败");
    }
}

